﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FifaChamps.API.Data
{
    public static partial class DataManager
    {
        public static List<Stage> Get_Stages(int tournamentID)
        {
            using (var db = DataManager.FifaDB())
            {
                var result = db.FCT_Tournament_Stages.Where(ts => ts.TournamentID.Value == tournamentID).Select(ts => Stage.Factory(ts.ID, ts.TournamentID.Value, ts.StageName, ts.StartDate, ts.EndDate)).ToList();
                return result;
            }
        }

        public static int Add_Stage(int tournamentid, string stagename, DateTime? startdate, DateTime? enddate, int typeid)
        {
            using (var db = DataManager.FifaDB())
            {
                var result = db.SP_Add_Tournament_Stage(tournamentid, stagename, startdate, enddate, typeid).First();
                return result.TStageID.Value;
            }
        }

        public static int Update_Stage(int stageid, int tournamentid, string stagename, DateTime? startdate, DateTime? enddate, int typeid)
        {
            using (var db = DataManager.FifaDB())
            {
                var result = db.SP_Update_Tournament_Stage(stageid, tournamentid, stagename, startdate, enddate, typeid).First();
                return result.TStageID.Value;
            }
        }

        public static int Add_StagePlayer(int stageid, int playerid)
        {
            using (var db = DataManager.FifaDB())
            {
                var result = db.SP_Add_Tournament_Stage_Player(stageid, playerid).First();
                return result.StagePlayerID.Value;
            }
        }

        public static void Remove_StagePlayer(int stage_playerid)
        {
            using (var db = DataManager.FifaDB())
            {
                db.SP_Remove_Tournament_Stage_Player(stage_playerid);
            }
        }

        public static void Remove_StagePlayer(int stageid, int playerid)
        {
            using (var db = DataManager.FifaDB())
            {
                var results = (from sp in db.FCT_Tournament_Stage_Players
                               where sp.StageID == stageid && sp.PlayerID == playerid
                               select sp).ToList();

                results.ForEach(rs => Remove_StagePlayer(rs.ID));
            }
        }

        public static List<StageType> Get_StageTypes()
        {
            List<StageType> types = new List<StageType>();

            using (var db = DataManager.FifaDB())
            {
                db.FCT_StageTypes.ToList().ForEach(tt => types.Add(new StageType(tt.ID, tt.Type)));
            }

            return types;
        }

        public static List<int> Get_StagePlayers(int stageID)
        {
            List<int> players = new List<int>();

            using (var db = DataManager.FifaDB())
            {
                players = db.FCT_Tournament_Stage_Players.Where(sp => sp.StageID == stageID).Select(sp => sp.PlayerID).ToList();
            }

            return players;
        }
    }
}
